package com.engine.devcom.mode.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.devcom.mode.util.WorkflowUtils;
import com.wbi.util.Util;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.hrm.User;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : hmy
 * @since : 2025/6/6
 */
public class ModifyFieldValueCmd extends AbstractCommonCommand<Map<String, Object>> {
    public ModifyFieldValueCmd(Map<String, Object> params, User user) {
        this.params = params;
        this.user = user;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        String requestId = Util.null2String(params.getOrDefault("requestid", ""));
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        if (StringUtils.isBlank(requestId)) {
            result.put("code", -2);
            result.put("msg", "request is empty!");
            return result;
        }
        String workflowId = Util.null2String(params.getOrDefault("workflowid", ""));
         result.put("code", 0);
        if (StringUtils.isBlank(workflowId)) {
            result.put("code", -2);
            result.put("msg", "workflowId is empty!");
            return result;
        }
        String fieldName = Util.null2String(params.getOrDefault("name", ""));
        if (StringUtils.isBlank(fieldName)) {
            result.put("code", -2);
            result.put("msg", "name is empty!");
            return result;
        }

        String value = Util.null2String(params.getOrDefault("value", ""));

        String tableName = WorkflowUtils.getTableName(workflowId);

        RecordSet rs = new RecordSet();
        String sql = "update " + tableName + " set " + fieldName + " = ? where requestid = ?";
        boolean b = rs.executeUpdate(sql, value, requestId);
        rs.writeLog(">>>>>>ModifyFieldValueCmd  sql: " + sql);
        result.put("code", 0);
        result.put("msg", b);
        return result;
    }
}
